package com.example.Set;

import java.util.*;

public class TestSet {
    public static void main(String[] args) {
        //method1();
        //method2();

        TreeSet<Teacher> tss = new TreeSet<>(new Comparator<Teacher>() {
            @Override
            public int compare(Teacher o1, Teacher o2) {
                //o1表示现在要存入的那个元素
                //o2表示已经存入到集合中的元素

                //主要条件
                int result = o1.getAge() - o2.getAge();
                //次要条件
                result = result == 0 ? o1.getName().compareTo(o2.getName()) : result;
                return result;
            }
        });
        Teacher s1 = new Teacher("zhangsan", 21);
        Teacher s2 = new Teacher("lisi", 19);
        Teacher s3 = new Teacher("wangwu", 20);
        Teacher s4 = new Teacher("zhaoliu", 19);
        tss.add(s1);
        tss.add(s2);
        tss.add(s3);
        tss.add(s4);
        System.out.println(tss);
        //[Student{name='lisi', age=19}, Student{name='zhaoliu', age=19}, Student{name='wangwu', age=20}, Student{name='zhangsan', age=21}]

    }

    private static void method2() {
        TreeSet<Integer> ts = new TreeSet<>();
        ts.add(5);
        ts.add(3);
        ts.add(4);
        ts.add(1);
        ts.add(2);
        System.out.println(ts); //[1, 2, 3, 4, 5]

        TreeSet<Student> tss = new TreeSet<>();
        Student s1 = new Student("zhangsan", 21);
        Student s2 = new Student("lisi", 19);
        Student s3 = new Student("wangwu", 20);
        Student s4 = new Student("zhaoliu", 19);
        Student s5 = new Student("qingqi", 19);
        tss.add(s1);
        tss.add(s2);
        tss.add(s3);
        tss.add(s4);
        tss.add(s5);
        System.out.println(tss);//[Student{name='李四', age=19}, Student{name='王五', age=20}, Student{name='张三', age=21}]
        //[Student{name='lisi', age=19}, Student{name='qingqi', age=19}, Student{name='zhaoliu', age=19}, Student{name='wangwu', age=20}, Student{name='zhangsan', age=21}]
    }

    private static void method1() {
        Set<String> set = new TreeSet<>();
        set.add("ccc");
        set.add("aaa");
        set.add("aaa");
        set.add("bbb");

        Iterator<String> it = set.iterator();
        while (it.hasNext()) {
            System.out.println(it.next());
        }
        System.out.println("--------------------");
        for (String s : set) {
            System.out.println(s);
        }
    }
}

class Student implements Comparable<Student> {
    private String name;
    private int age;

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public int compareTo(Student o) {
        //按照对象的年龄进行排序
        //主要判断条件
        int result = this.age - o.age;
        //次要判断条件
        result = result == 0 ? this.name.compareTo(o.getName()) : result;
        return result;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Student student = (Student) o;
        return age == student.age && Objects.equals(name, student.name);
    }

    //我们可以对Obiect类中的hashcode方法进行重写
    //在重写之后，就一般是根据对象的属性值来计算哈希值的。
    //此时跟对象的地址值就没有任何关系了。
    @Override
    public int hashCode() {
        int result = Objects.hashCode(name);
        result = 31 * result + age;
        return result;
    }
}


class Teacher {
    private String name;
    private int age;

    public Teacher() {
    }

    public Teacher(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

}